import request from "@/utils/request";

// const AUTH_BASE_URL = "/api/v1/auth";
const AUTH_BASE_URL = "";

const AuthAPI = {
  /** 登录接口*/

  login(data: LoginFormData) {
    // const formData = new FormData();
    // formData.append("username", data.username);
    // formData.append("password", data.password);
    // formData.append("captchaKey", data.captchaKey);
    // formData.append("captchaCode", data.captchaCode);
    console.log(data, "接口调用");
    return request<any, LoginResult>({
      url: `${AUTH_BASE_URL}/login`,
      method: "post",
      data,
      // headers: {
      //   "Content-Type": "multipart/form-data",
      // },
    });
  },

  // login(data: LoginFormData) {
  //   const formData = new FormData();
  //   formData.append("username", data.username);
  //   formData.append("password", data.password);
  //   formData.append("captchaKey", data.captchaKey);
  //   formData.append("captchaCode", data.captchaCode);
  //   return request<any, LoginResult>({
  //     url: `${AUTH_BASE_URL}/login`,
  //     method: "post",
  //     data: formData,
  //     headers: {
  //       "Content-Type": "multipart/form-data",
  //     },
  //   });
  // },

  /** 刷新 token 接口*/
  refreshToken(refreshToken: string) {
    return request<any, LoginResult>({
      // url: `${AUTH_BASE_URL}/refresh-token`,
      url: `${AUTH_BASE_URL}/refreshToken`,
      method: "post",
      data: { refreshToken },
      // params: { refreshToken },
      headers: {
        Authorization: "no-auth",
      },
    });
  },

  /** 退出登录接口 */
  logout() {
    return request({
      url: `${AUTH_BASE_URL}/logout`,
      method: "post",
      //method: "delete",
    });
  },

  /** 获取验证码接口*/
  getCaptcha() {
    const mockData = {
      code: "00000",
      data: {
        captchaKey: "191bf4fa170349ada7dc6dc564cea144",
        captchaBase64:
          "",
      },
      msg: "一切ok",
    };
    return request<any, CaptchaInfo>({
      url: `${AUTH_BASE_URL}/captcha`,
      method: "get",
    });
  },
};

export default AuthAPI;

/** 登录表单数据 */
export interface LoginFormData {
  /** 用户名 */
  username: string;
  /** 密码 */
  password: string;
  /** 验证码缓存key */
  // captchaKey: string;
  // /** 验证码 */
  // captchaCode: string;
  // /** 记住我 */
  rememberMe: boolean;
}

/** 登录响应 */
export interface LoginResult {
  /** 访问令牌 */
  accessToken: string;
  /** 刷新令牌 */
  refreshToken: string;
  /** 令牌类型 */
  tokenType: string;
  /** 过期时间(秒) */
  expiresIn: number;
}

/** 验证码信息 */
export interface CaptchaInfo {
  /** 验证码缓存key */
  captchaKey: string;
  /** 验证码图片Base64字符串 */
  captchaBase64: string;
}
